<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="标题" prop="title">
          <el-input
            v-model="queryParams.title"
            placeholder="请输入标题"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="广告状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择广告状态" clearable>
            <el-option
              v-for="dict in dict.type.ad_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['maincode:advertisements:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['maincode:advertisements:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="advertisementsList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="发布人" align="center" prop="createBy" />
        <el-table-column label="公告id" align="center" prop="noticeId" ></el-table-column>
        <el-table-column label="公告标题" align="center" prop="noticeTitle" width="180"></el-table-column>
        <el-table-column label="公告内容" align="center" prop="noticeContent" width="180"></el-table-column>
        <el-table-column label="公告状态" align="center" prop="startTime" width="180">
          <template slot-scope="scope">
            <div v-if="scope.row.status == 0">正常</div>
            <div v-if="scope.row.status == 1">关闭</div>
          </template>
        </el-table-column>
        <el-table-column label="公告类型" align="center" prop="startTime" width="180">
          <template slot-scope="scope">
            <div v-if="scope.row.noticeType == 1">通知</div>
            <div v-if="scope.row.noticeType == 2">公告</div>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['maincode:advertisements:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['maincode:advertisements:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改广告对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="公告标题">
            <el-input v-model="form.title" type="textarea" placeholder="请输入内容" />
          </el-form-item>

          <el-form-item label="公告类型">
            <el-radio-group v-model="form.type" size="large">
                <el-radio-button label="通知" value="1" />
                <el-radio-button label="公告" value="2" />
            </el-radio-group>
          </el-form-item>
          <el-form-item label="公告状态">
            <el-radio-group v-model="form.clicks" size="large">
                <el-radio-button label="正常" value="0" />
                <el-radio-button label="关闭" value="1" />
            </el-radio-group>
          </el-form-item>
          <el-form-item label="公告内容">
            <el-input v-model="form.content" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
  
      <!-- 添加或修改广告对话框 -->
      <el-dialog :title="title" :visible.sync="updata" width="1000px" append-to-body>
        <el-form ref="updataForm" :model="updataForm" :rules="rules" label-width="80px">
          <el-form-item label="公告标题">
            <el-input v-model="updataForm.title" type="textarea" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="公告类型">
            <el-radio-group v-model="updataForm.type" size="large">
                <el-radio-button label="通知" value="1" />
                <el-radio-button label="公告" value="2" />
            </el-radio-group>
          </el-form-item>
          <el-form-item label="公告状态">
            <el-radio-group v-model="updataForm.clicks" size="large">
                <el-radio-button label="正常" value="0" />
                <el-radio-button label="关闭" value="1" />
            </el-radio-group>
          </el-form-item>
          <el-form-item label="公告内容">
            <el-input v-model="updataForm.content" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitupdataForm">确 定</el-button>
          <el-button @click="updata = false">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { getText,addText,deleteText,updateText,getmainText } from '@/api/store/index'
  import dayjs from 'dayjs';
  export default {
    name: "Advertisements",
    dicts: ['ad_status'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 广告表格数据
        advertisementsList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        updata:false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          title: null,
          content: null,
          link: null,
          startTime: null,
          expirationTime: null,
          clicks: null,
          status: null,
          imageUrl: null,
        },
        Idproduct:0,
        // 表单参数
        form: {},
        updataForm:{
          type:'',
          title:'',
          clicks:'',
          content:''
        },
        NewData:{},
        // 表单校验
        rules: {
          title: [
            { required: true, message: "广告标题不能为空", trigger: "blur" }
          ],
          content: [
            { required: true, message: "价格不能为空", trigger: "blur" }
          ],
          link: [
            { required: true, message: "库存需要填写", trigger: "blur" }
          ],
          startTime: [
            { required: true, message: "上线时间不能为空", trigger: "blur" }
          ],
          expirationTime: [
            { required: true, message: "过期时间不能为空", trigger: "blur" }
          ],
          status: [
            { required: true, message: "广告状态不能为空", trigger: "change" }
          ],
        },
        
      };
    },
    created() {
      this.getList();
    },
    computed:{
    },
    methods: {
      /** 查询广告列表 */
      getList() {
        this.loading = true;
        getText().then(response => {
          this.advertisementsList = response.data;
          // this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          adId: null,
          title: null,
          content: null,
          link: null,
          startTime: null,
          expirationTime: null,
          clicks: null,
          status: null,
          imageUrl: null,
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.adId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加商品";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.updata = true
        this.Idproduct = row
        getmainText(row.noticeId).then(res => {
            this.updataForm.type = res.data.noticeType == '1' ? '通知' : '公告'
            this.updataForm.clicks = res.data.status == '0' ? '正常' : '关闭'
            this.updataForm.content = res.data.noticeContent
            this.updataForm.title = res.data.noticeTitle
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            addText({
              noticeTitle:this.form.title,
              noticeType: this.form.type == '通知' ? 1 : 2,
              noticeContent: this.form.content,
              status: this.form.clicks == '正常' ? 0 : 1
            }).then(res => {
                this.$modal.msgSuccess("增加成功");
                this.open = false;
                this.getList();
            })
          }
        });
      },
      submitupdataForm(){
        this.$refs["updataForm"].validate(valid => {
          if (valid) {
            let localTypeI; // ✅ Create a new variable
            if(this.updataForm.type == '待付款'){
                localTypeI = 0
            } else if(this.updataForm.type == '待发货'){
                localTypeI = 1
            }else if(this.updataForm.type == '待收货'){
              localTypeI = 2
            }else if(this.updataForm.type == '完成订单'){
              localTypeI = 3
            }
            updateText({
                noticeId:this.Idproduct.noticeId,
                noticeTitle:this.updataForm.title,
                noticeType:this.updataForm.type == '通知' ? '1' :'2',
                noticeContent:this.updataForm.content,
                status:this.updataForm.clicks == '正常' ? '0' : '1'
            }).then(res => {
                this.$modal.msgSuccess("修改成功");
                this.updata = false;
                this.updataForm = {}
                this.getList();
            })
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const adIds = row.noticeId
        this.$modal.confirm('是否确认删除该订单').then(function() {
          return deleteText(adIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('maincode/advertisements/export', {
          ...this.queryParams
        }, `advertisements_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>
  